package com.ctcemti.mdm.project.sys.service.impl;

import com.ctcemti.mdm.project.sys.dao.InfDictItemMapper;
import com.ctcemti.mdm.project.sys.dao.InfDictSortMapper;
import com.ctcemti.mdm.project.sys.entity.InfDictSort;
import com.ctcemti.mdm.project.sys.entity.formdata.BaseReq;
import com.ctcemti.mdm.project.sys.service.InfDictSortService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class InfDictSortServiceImpl implements InfDictSortService {
    @Autowired
    private InfDictSortMapper InfDictSortMapper;

    @Autowired
    private InfDictItemMapper InfDictItemMapper;


    @Transactional
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return InfDictSortMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    @Override
    public int insert(InfDictSort record) {
        return InfDictSortMapper.insert(record);
    }

    @Transactional
    @Override
    public int insertSelective(InfDictSort record) {
        return InfDictSortMapper.insertSelective(record);
    }

    @Override
    public InfDictSort selectByPrimaryKey(Integer id) {
        return InfDictSortMapper.selectByPrimaryKey(id);
    }

    @Transactional
    @Override
    public int updateByIdSelective(InfDictSort record) {
        return InfDictSortMapper.updateByIdSelective(record);
    }

    @Transactional
    @Override
    public int updateByPrimaryKey(InfDictSort record) {
        return InfDictSortMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<InfDictSort> selectAllInfdictsort() {
        return InfDictSortMapper.selectAll();
    }

    @Override
    public List<InfDictSort> selectByPid(String s) {
        Example example = new Example(InfDictSort.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pid",s);
        example.setOrderByClause("sort");//排序
        return InfDictSortMapper.selectByExample(example);
    }

    @Override
    public List<InfDictSort> selectInfdictsort(InfDictSort InfDictSort) {
        return InfDictSortMapper.select(InfDictSort);
    }

    @Override
    public List<InfDictSort> selectInfdictsortByInfdictsortName(String name){
        return InfDictSortMapper.selectInfdictsortByInfdictsortName(name);
    }

    @Transactional
    @Override
    public int deleteByPrimaryKeys(String ids) {
        String idarray[] = ids.split(",");
        InfDictItemMapper.deleteByInfDictSortId(idarray);//删除子类字典详细类
        InfDictSortMapper.deleteByPid(idarray);//删除子类字典法分类
        return InfDictSortMapper.deleteByIds(ids);
    }

    @Override
    public PageInfo<InfDictSort> selectListByPid(String id, BaseReq baseReq) {
        PageHelper.startPage(baseReq.getPageIndex(),baseReq.getPageSize(),"sort");
        Example example = new Example(InfDictSort.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pid",id);
        List<InfDictSort> list = InfDictSortMapper.selectByExample(example);
        PageInfo<InfDictSort> pageInfo =  new PageInfo<InfDictSort>(list);
        return pageInfo;
    }
}
